class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ordernum: '',
            data: "",
            createDate: '',
            paytime: "",
            goodsinfo: '',
            avatarUrl: '',
            addressinfo: '',
            postage:"",
            userlogisticshtml:[],
            shopkeeperhtml:[],
        };
    }

    formatlist(list) {
        var listitem = [];
        list.forEach((x, i) => {
            let src = ''
                if (x.goodsimg.indexOf('http') == -1) {
                    src = `${API_HEAD}imgs/` + x.goodsimg
                } else {
                    src =x.goodsimg
                }
           
            listitem.push(
                <tr>
                    <td className="customerbox">
                        <div className="customer">
                            <img src={src} />
                            <p>{x.goodsname}</p>
                        </div>
                    </td>
                    <td>x <span>{x.paynum}</span></td>
                    <td>￥{(x.promotionprice / 100).toFixed(2)}</td>
                </tr>
            )
        });
        this.setState({ listitem, list, originallist: list, })
    }
    componentDidMount() {
        // let {postage}=this.state
        // basegetSettings(res=>{
        //     if(res.issuccess==1){
        //         postage=res.data.PostFee
        //         this.setState({postage},()=>{
                    let ordernum = GetQueryString('ordernum')
                    this.setState({ ordernum }, () => {
                        this.freshlist()
                    })
                // })
        //     }
        // })
        // if (id != 'null' && id) {
        //     this.setState({ id }, () => {
        //         this.freshlist()
        //     })
        // } else {
        //     let { assemble, boutique, hotbuy, newproducts, recommend, setmeal } = this.state
        //     this.setState({ name: '', groupprice: '', starttime: '', endtime: '', buymax: '', personlimit: '', timelimit: '', cid: '0' })
        // }
    }
    freshlist() {
        let { ordernum, createDate, paytime, goodsinfo } = this.state,that=this;
        findbynumber(ordernum, res => {
            if (res.issuccess == 1) {
                let userlogistics=res.userlogistics==''?"":JSON.parse(decodeurl(res.userlogistics)),
                shopkeeper=res.shopkeeper==''?"":JSON.parse(decodeurl(res.shopkeeper))
                // addressinfo=res.data.addressinfo==null?"":JSON.parse(decodeurl(res.data.addressinfo)),
                console.log(userlogistics,'userlogisticsuserlogistics');
                console.log(shopkeeper,'shopkeepershopkeepershopkeeper');
                if(userlogistics!=''){
                    that.formatuserlogistics(userlogistics)
                }
                if(shopkeeper!=''){
                    that.formatshopkeeper(shopkeeper)
                }
                
                that.setState({data:res.data,books:res.books},()=>{
                    that.formatlist(res.books)
                })
            }
        })

    }
    formatuserlogistics(data){
        let userlogisticshtml=[]
        if(data.Success){
            data.Traces.forEach(x=>{
                userlogisticshtml.push(
                    <div>
                        <p>{x.AcceptStation}</p>
                        <p>{x.AcceptTime}</p>
                    </div>
                )
            })
        }
        this.setState({userlogisticshtml})
        
    }
    formatshopkeeper(data){
        let shopkeeperhtml=[]
        if(data.Success){
            data.Traces.forEach(x=>{
                shopkeeperhtml.push(
                    <div>
                        <p>{x.AcceptStation}</p>
                        <p>{x.AcceptTime}</p>
                    </div>
                )
            })
        }
        this.setState({shopkeeperhtml})
        
    }
    // 用户确认收货
    userreceipt(){
        let {data}=this.state
        setgoodsorderreceived(data.ordernum,res=>{
            if(res.issuccess==1){
                alert('确认收货')
                this.freshlist() 
            }else{
                alert(res.msg)
            }
        })
    }
    // 店家确认收货
    // shopreceipt(){
    //     let {data}=this.state
    //     setordersolving(data.ordernum,res=>{
    //         if(res.issuccess==1){
    //             alert('确认收货')
    //             this.freshlist() 
    //         }else{
    //             alert(res.msg)
    //         }
    //     })
    // }

    // inputchange(prop, e) {
    //     // let { target } = this.state;
    //     // target[prop] = e.target.value
    //     // this.setState({ target })
    //     this.setState({ [prop]: e.target.value })
    // }
    toback() {
        window.location.href = "./goodsorder.html";
    }
    render() {
        let { data, createDate, paytime, listitem,userlogisticshtml,shopkeeperhtml } = this.state;

        return (
            <div className="breadLinebox">
                <div className='content_wrap panel-default content_wrap'>
                    <div className="breadLine">
                        <div className="img">
                        当前位置 > <span onClick={this.toback.bind(this)} style={{ cursor: 'pointer' }}>优选购订单</span> > 订单详情
                        </div>
                    </div>
                    <div className="content form">
                        <div className="formtitle">
                            {/* <p>{this.state.state == "END" || this.state.state == "NOWORK" ? "查看" : this.state.id > 0 ? "编辑" : "新建"}</p> */}
                        </div>
                        <div className="formbox">
                            <div className="formcontent">
                                {/* 订单信息 */}
                                <div>
                                    <div className="ordertitle">
                                        订单信息
                                </div>
                                    <div className="ordercont">
                                        <p className="start">{getstatecontent(data.state)}
                                            {/* <span>（已成团）</span> */}
                                        </p>

                                        <div className="input-group flex">
                                            <span className="input-group-addon">订单编号：</span>
                                            <span className="inputcontent">{data.ordernum}</span>
                                        </div>
                                        <div className="input-group flex">
                                            <span className="input-group-addon">快递公司名称：</span>
                                            <span className="inputcontent">{data.express_name?data.express_name:'无'}</span>
                                        </div>
                                        <div className="input-group flex">
                                            <span className="input-group-addon">运单号：</span>
                                            <span className="inputcontent">{data.express_num?data.express_num:'无'}</span>
                                        </div>
                                        <div className="input-group flex">
                                            <span className="input-group-addon">下单时间：</span>
                                            <span className="inputcontent">{formatDate(new Date(data.createDate))}</span>
                                        </div>
                                        <div className="input-group flex">
                                            <span className="input-group-addon">收货人姓名：</span>
                                            <span className="inputcontent">{data.borrow_username}</span>
                                        </div>
                                        <div className="input-group flex">
                                            <span className="input-group-addon">收货人电话：</span>
                                            <span className="inputcontent">{data.phone}</span>
                                        </div>
                                        {/* <div className="input-group flex">
                                            <span className="input-group-addon">押金：</span>
                                            <span className="inputcontent">{(data.deposit/100).toFixed(2)}</span>
                                        </div> */}
                                        <div className="input-group flex">
                                            <span className="input-group-addon">付款时间：</span>
                                            <span className="inputcontent">{formatDate(new Date(data.payDate))}</span>
                                        </div>
                                        <div className="input-group flex">
                                            <span className="input-group-addon">收货地址：</span>
                                            <span className="inputcontent">{data.addressinfo}</span>
                                        </div>
                                        <div className="width100">
                                        <div className="input-group flex width100">
                                            <span className="input-group-addon">备注：</span>
                                            <span className="inputcontent">{data.describe ? data.describe : "无"}</span>
                                        </div>
                                        </div>
                                    </div>
                                </div>
                                {/* end 订单信息 */}
                                {/* 客户物流信息 */}
                                <div style={{display:userlogisticshtml.length>0?'':'none'}}>
                                    <div className="ordertitle">
                                        客户物流信息
                                </div>
                                    <div className="logistics">
                                        {userlogisticshtml}
                                    </div>
                                    <div className="submitbox sublogistics">
                                        <button type="button" onClick={this.userreceipt.bind(this)} className={`submitbutton   ${data.state=='SHIPPED'?'':'hui'}`}>{data.state=='SHIPPED'?'用户确认收货':'用户已收货'}</button>
                                    </div>
                                </div>
                                {/* end 客户物流信息 */}
                                {/* 物品退回物流信息 */}
                                <div style={{display:shopkeeperhtml.length>0?'':'none'}}>
                                    <div className="ordertitle">
                                    物品退回物流信息
                                </div>
                                    <div className="logistics">
                                        {shopkeeperhtml}
                                    </div>
                                    {/* <div className="submitbox sublogistics">
                                        <button type="button" onClick={this.shopreceipt.bind(this)} className={`submitbutton   ${data.state=='SOLVING'||data.state=='SOLVED'?'hui':''}`}>{data.state=='SOLVING'||data.state=='SOLVED'?'店家已收货':'店家确认收货'}</button>
                                    </div> */}
                                </div>
                                {/* end 客户物流信息 */}
                                {/* 消费信息 */}
                                <div>
                                    <div className="ordertitle">
                                        消费信息
                                </div>
                                    <div className="ordercont">
                                        <table className="tab">
                                            <thead>
                                                <tr>
                                                    <th>商品</th>
                                                    <th>数量</th>
                                                    <th>金额</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {listitem}
                                            </tbody>
                                        </table>
                                        <div className="intotal">
                                            <div>
                                                <p><span>应收金额：</span><span>￥{(data.total / 100).toFixed(2)}</span></p>
                                                <p><span>实收金额：</span><span>￥{(data.cost/ 100).toFixed(2)}</span></p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                {/* end 消费信息 */}
                            </div>
                            <div className="submitbox">
                                <button type="button" onClick={this.toback.bind(this)} className="submitbutton">返回</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        )


    }
}





ReactDOM.render(
    <div>
        <TopLine />
        <div id='body'>
            <LeftMenu code='goodsorder' height="order" />
            <Main />
        </div>
    </div>
    // <Barrage/>
    , $('.container')[0]
)